#include <stdio.h>
#include <string.h>

#include <raw_api.h>
#include <mm/raw_page.h>
#include <mm/raw_malloc.h>
#include <fifo.h>
#include "Uart_fifo.h"
#include "2440lib.h"
#include "timer_bsp.h"

#include "common.h"

#define  UART_DRIVER_MODEL_STK_SIZE 2048
PORT_STACK uart_driver_model_stack[UART_DRIVER_MODEL_STK_SIZE];
RAW_TASK_OBJ  uart_driver_model_obj;


extern void global_mem_init(unsigned int start_mem, unsigned int end_mem);


#if (UART_RECEIVE_WAY > 0)
#if 1
	void *queue_storge;
	MEM_POOL MEM_FOR_UART;
	RAW_QUEUE uart_queue;

	void Uart_driver_test(void * pParam)
	{
		void *memory;
		
		Uart0Init_fifo();
		Uart_Printf("Uart0Init_fifo...\n");
		global_mem_init(0x30000000, 0x32000000);		
		queue_storge = raw_page_allocate(4);
		raw_queue_create(&uart_queue, "driver_queue", (RAW_VOID **)queue_storge, 2048);
		Uart_Printf("raw_queue_create...\n");		
		memory = raw_page_allocate(50);
		raw_block_pool_create(&MEM_FOR_UART,"Mem_pool",64,(void*)memory, 50 * 2048);
		Uart_Printf("raw_block_pool_create...\n");
		Timer0Init();
		Uart_Printf("Timer0Init...\n");
		while(1)
		{
			Main_Menu();
		}
	}
#endif
#else
	
	RAW_SEMAPHORE s1;	
	struct raw_fifo fifo;

	void Uart_driver_test(void * pParam)
	{
		global_mem_init(0x30000000, 0x32000000);
		
		Uart0Init_fifo();
		Uart_Printf("Uart0Init_fifo...\n");
		fifo_alloc(&fifo, 2048);
		Uart_Printf("fifo_alloc...\n");
		raw_semaphore_create(&s1, "sem1", 0);
		Uart_Printf("raw_semaphore_create...\n");
		Timer0Init();
		Uart_Printf("Timer0Init...\n");
		while(1)
		{
			Main_Menu();
		}
	}

#endif


extern RAW_U8 test_started;
void uart_driver_model()
{

	if (test_started) {

		return;

	}

	test_started = 1;
	
	raw_task_create(&uart_driver_model_obj, (RAW_U8  *)"uart_driver_model", 0,
	                         2, 0,  uart_driver_model_stack, 
	                         UART_DRIVER_MODEL_STK_SIZE ,  Uart_driver_test, 1); 
}


